package com.tinyspot.concrete.gateway.config;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 命名以 GatewayFilterFactory 结尾
 * 名称是 CheckAuth
 */
@Component
public class CheckAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CheckAuthGatewayFilterFactory.Config> {

    public CheckAuthGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            System.out.println("读取配置：" + config.getName());
            ServerHttpRequest request = exchange.getRequest().mutate().build();
            return chain.filter(exchange.mutate().request(request).build());
        };
    }

    /**
     * 指定参数顺序和数量
     * @return
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("name", "enabled");
    }

    /**
     * 自定义配置类
     */
    @Data
    public static class Config {
        private String name;
        private boolean enabled;
    }
}
